package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.auth.api.AuthServiceFeign;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.exhibitor.ExhibitorEmployeeCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.imaster.dao.ExhibitorEmployeeMapper;
import com.eastfair.imaster.dto.ExhibitorEmployeeDTO;
import com.eastfair.imaster.entity.ExhibitorEmployee;
import com.eastfair.imaster.service.ExhibitorEmployeeService;
import com.eastfair.imaster.vo.ExhibitorEmployeeVo;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 展商员工
 * </p>
 *
 * @author ligang
 * @date 2021-09-07
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ExhibitorEmployeeServiceImpl extends SuperCacheServiceImpl<ExhibitorEmployeeMapper, ExhibitorEmployee> implements ExhibitorEmployeeService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;

    @Autowired
    AuthServiceFeign authServiceFeign;

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(ExhibitorEmployee entity) {
        UpdateWrapper<ExhibitorEmployee> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(ExhibitorEmployee::getProjectId, entity.getProjectId()).eq(ExhibitorEmployee::getId, entity.getId());
        entity.setProjectId(null);
        //删除缓存
        delCache(entity);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitorEmployee model) {
        //项目id
        model.setProjectId(ContextUtil.getProjectId());
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(model.getProjectId()));
        return R.successDef();
    }

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new ExhibitorEmployeeCacheKeyBuilder();
    }

    @Override
    public R addEmployeeOfExhibitor(ExhibitorEmployeeDTO exhibitorEmployeeDTO) {
        ExhibitorEmployee exhibitorEmployee = new ExhibitorEmployee();
        exhibitorEmployee.setExhibitionId(exhibitorEmployeeDTO.getExhibitionId());
        exhibitorEmployee.setExhibitorId(exhibitorEmployeeDTO.getExhibitorId());
        exhibitorEmployee.setEmployeeId(exhibitorEmployeeDTO.getEmployeeId());
        List<ExhibitorEmployee> exhibitorEmployeeList = list(exhibitorEmployee, null);
        if (exhibitorEmployeeList != null && !exhibitorEmployeeList.isEmpty()) {
            //已经存在
            return R.success();
        }

        //新增
        ExhibitorEmployee exhibitorEmployee1 = new ExhibitorEmployee();
        BeanUtils.copyProperties(exhibitorEmployeeDTO, exhibitorEmployee1);
        exhibitorEmployee1.setProjectId(ContextUtil.getProjectId());
        save(exhibitorEmployee1);
        //组织角色：同步员工到展商员工
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setResourceId(exhibitorEmployee1.getCompanyId());
        organizationDTO.setUserIds(Arrays.asList(exhibitorEmployee1.getUserAccountId()));
        authServiceFeign.addUseridOfExhibitionCompany(organizationDTO);
        return R.success();
    }

    @Override
    public R removeEmployeeOfExhibitor(Long exhibitoUserId) {
        List<ExhibitorEmployee> exhibitorEmployeeList = list(null, Arrays.asList(exhibitoUserId));
        if (exhibitorEmployeeList != null && !exhibitorEmployeeList.isEmpty()) {
            ExhibitorEmployee exhibitorEmployee = exhibitorEmployeeList.get(0);
            exhibitorEmployee.setIsDeleted(BusinessConstant.DELETE_YES);
            updateByIdOfShard(exhibitorEmployee);
            //组织角色：同步员工到展商员工
            OrganizationDTO organizationDTO = new OrganizationDTO();
            organizationDTO.setResourceId(exhibitorEmployee.getCompanyId());
            organizationDTO.setUserIds(Arrays.asList(exhibitorEmployee.getUserAccountId()));
            authServiceFeign.removeUseridOfExhibitionCompany(organizationDTO);
        }
        return R.success();
    }

    @Override
    public List<ExhibitorEmployee> list(ExhibitorEmployee exhibitorEmployee, List<Long> ids) {
        //查询
        QueryWrapper<ExhibitorEmployee> queryWrapper = new QueryWrapper<>();
        if (exhibitorEmployee != null) {
            if (exhibitorEmployee.getId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getId, exhibitorEmployee.getId());
            }
            if (exhibitorEmployee.getExhibitionId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getExhibitionId, exhibitorEmployee.getExhibitionId());
            }
            if (exhibitorEmployee.getExhibitorId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getExhibitorId, exhibitorEmployee.getExhibitorId());
            }
            if (exhibitorEmployee.getCompanyId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getCompanyId, exhibitorEmployee.getCompanyId());
            }
            if (exhibitorEmployee.getEmployeeId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getEmployeeId, exhibitorEmployee.getEmployeeId());
            }
            if (exhibitorEmployee.getUserAccountId() != null) {
                queryWrapper.lambda().eq(ExhibitorEmployee::getUserAccountId, exhibitorEmployee.getUserAccountId());
            }
        }
        if (ContextUtil.getProjectId() != null) {
            queryWrapper.lambda().eq(ExhibitorEmployee::getProjectId, ContextUtil.getProjectId());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ExhibitorEmployee::getId, ids);
        }
        queryWrapper.lambda().eq(ExhibitorEmployee::getIsDeleted, BusinessConstant.DELETE_NO);
        return list(queryWrapper);
    }

    @Override
    public List<ExhibitorEmployeeVo> queryUser(Long exhibitorId) {
        ExhibitorEmployee exhibitorEmployee = new ExhibitorEmployee();
        exhibitorEmployee.setExhibitorId(exhibitorId);
        List<ExhibitorEmployee> exhibitorEmployeeList = list(exhibitorEmployee, null);
        List<ExhibitorEmployeeVo> exhibitorEmployeeVoList = ConvertUtil.convertList(exhibitorEmployeeList, ExhibitorEmployeeVo.class);
        return exhibitorEmployeeVoList;
    }
}
